package app.netant.book.base;

import org.jsoup.select.Elements;

import cn.lily.com.lang.Basic;
import app.netant.NetAntBase;

public abstract class BaseDetail extends NetAntBase {
	
	protected BaseEntity entity;
	
	protected String preHref;

	public BaseDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(BaseEntity entity){
		this.entity = entity;
	}
	
	public abstract void analyseTitle();
	
	public abstract void analyseTags();
	
	public abstract void analyseMemo();
	
	public abstract void analyseHref();
	
	public abstract void analyseType();
	
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
		this.entity.download();
		this.entity.insert();
		
	}
	
	public void test(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		this.analyseMemo();
		this.analyseHref();
		
	}
	
	public void splitTagsPlus(String splits,String tagAlls,boolean flag){
		
		String[] tagm = Basic.split(tagAlls, splits.split(","));
		String keys = "";
		String vals = "";
		
		for(int i=0;i<tagm.length;i++){
			
			String tmp = tagm[i].trim();
			
			if(i==0){
				keys += "," + tmp.replaceAll(" ", "");
			}
			else if(i==tagm.length-1){
				vals += "," + tmp.replaceAll(" ", "");
			}
			else{
				
				int first = tmp.indexOf(" ");
				if(first>0){
					keys += "," + tmp.substring(first).replaceAll(" ", "");
					vals += "," + tmp.substring(0,first).replaceAll(" ", "");
				}
				
			}
			
		}
		
		if(!"".equals(keys)){
			keys = keys.substring(1);
			vals = vals.substring(1);
		}
		
		String[] keyS = keys.split(",");
		String[] valS = vals.split(",");
		
		for(int j=0;j<keyS.length;j++){
			
			System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
			
			if(flag){
				this.entity.setTag(keyS[j], valS[j]);
			}
				
		}	
	}
	
	public void splitTags(String tagAlls,boolean flag){
		
		String[] tagm = tagAlls.split("：");
		String keys = "";
		String vals = "";
		
		for(int i=0;i<tagm.length;i++){
			
			String tmp = tagm[i].trim();
			
			if(i==0){
				keys += "," + tmp.replaceAll(" ", "");
			}
			else if(i==tagm.length-1){
				vals += "," + tmp.replaceAll(" ", "");
			}
			else{
				
				int first = tmp.indexOf(" ");
				if(first>0){
					keys += "," + tmp.substring(first).replaceAll(" ", "");
					vals += "," + tmp.substring(0,first).replaceAll(" ", "");
				}
				
			}
			
		}
		
		if(!"".equals(keys)){
			keys = keys.substring(1);
			vals = vals.substring(1);
		}
		
		String[] keyS = keys.split(",");
		String[] valS = vals.split(",");
		
		for(int j=0;j<keyS.length;j++){
			
			System.out.println("【Tag】"+keyS[j]+"="+valS[j]);
			
			if(flag){
				this.entity.setTag(keyS[j], valS[j]);
			}
				
		}	
	}
	
}
